//$URL: http://dasding.googlecode.com/svn/branches/testzeugs/data/data/src/main/java/de/piratech/dasding/data/User.java $
//$Id: User.java 89 2012-11-12 14:55:42Z amuthmann@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data;

import de.piratech.dasding.data.database.DataType;
import java.security.Principal;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.annotate.JsonIgnore;

/**
 * Dataobject for a User saved in the system
 *
 * @author deveth0
 */
@XmlRootElement
public class User extends AbstractDataObject implements Principal {

  /**
   * Enum with all valid user-roles
   */
  public enum USER_ROLE {

    /**
     * Simple user
     */
    USER,
    /**
     * Verified user
     */
    VERIFIED_USER,
    /**
     * Admin
     */
    ADMIN
  }
  private String mName;
  private String mFullname;
  private String mPassword;
  private String mPhone;
  private String mMobile;
  private String mMail;
  private Set<String> mInterests = new HashSet<String>();
  private Set<String> mInterestedIn = new HashSet<String>();
  private Set<ActionApplication> mActionApplications = new HashSet<ActionApplication>();
  private Set<USER_ROLE> mRoles = new HashSet<USER_ROLE>();

  @Override
  public String getName() {
    return mName;
  }

  public void setName(String pName) {
    this.mName = pName;
  }

  /**
   * @return fullname of the user
   */
  public String getFullname() {
    return mFullname;
  }

  /**
   * Sets the fullname
   *
   * @param pFullname
   */
  public void setFullname(String pFullname) {
    this.mFullname = pFullname;
  }

  /**
   * @return the encrypted password
   */
  public String getPassword() {
    return mPassword;
  }

  /**
   * sets the password (DOES NOT ENCRYPT IT!!!)
   *
   * @param pPassword
   */
  public void setPassword(String pPassword) {
    this.mPassword = pPassword;
  }

  /**
   * @return phone number
   */
  public String getPhone() {
    return mPhone;
  }

  /**
   * Sets the phonenumber
   *
   * @param pPhone
   */
  public void setPhone(String pPhone) {
    this.mPhone = pPhone;
  }

  /**
   * @return mobile number
   */
  public String getMobile() {
    return mMobile;
  }

  /**
   * Sets the mobile number
   *
   * @param pMobile
   */
  public void setMobile(String pMobile) {
    this.mMobile = pMobile;
  }

  /**
   * @return mailadress
   */
  public String getMail() {
    return mMail;
  }

  /**
   * Sets the mail adress
   *
   * @param pMail
   */
  public void setMail(String pMail) {
    this.mMail = pMail;
  }

  /**
   * @return all roles of the user
   */
  public Set<USER_ROLE> getRoles() {
    return mRoles;
  }

  /**
   * sets the roles
   *
   * @param pRoles
   */
  public void setRoles(Set<USER_ROLE> pRoles) {
    this.mRoles = pRoles;
  }

  /**
   * Adds a role for the user
   *
   * @param pRole
   */
  public void addRole(USER_ROLE pRole) {
    this.mRoles.add(pRole);
  }

  /**
   * Checks if the user has a certain role
   */
  public boolean hasRole(USER_ROLE pRole) {
    return this.mRoles.contains(pRole);
  }

  /**
   * @return all interests (ActionCategories) of the user
   */
  @XmlElementWrapper
  public Set<String> getInterests() {
    return mInterests;
  }

  /**
   * Sets the interests (ActionCategories)
   *
   * @param pInterests
   */
  public void setInterests(Set<String> pInterests) {
    this.mInterests = pInterests;
  }

  /**
   * adds a interest (ActionCategory)
   *
   * @param pActionCategoryId
   */
  public void addInterest(String pActionCategoryId) {
    this.mInterests.add(pActionCategoryId);
  }

  /**
   * Removes an Interest
   *
   * @param pActioncategoryId
   */
  public void removeInterest(String pActioncategoryId) {
    mInterests.remove(pActioncategoryId);
  }

  @XmlElementWrapper
  public Set<String> getInterestedIn() {
    return mInterestedIn;
  }

  /**
   * Sets the events the user is interested in
   *
   * @param pInterestedIn
   */
  public void setInterestedIn(Set<String> pInterestedIn) {
    this.mInterestedIn = pInterestedIn;
  }

  /**
   * Adds a event as interested in
   *
   * @param pEventId
   */
  public void addInterestedIn(String pEventId) {
    this.mInterestedIn.add(pEventId);
  }

  public void removeInterestedIn(String pEventId) {
    mInterestedIn.remove(pEventId);
  }

  /**
   * @return the action applications of the user
   */
  @XmlElementWrapper
  @XmlElement(name = "actionapplication")
  public Set<ActionApplication> getActionApplications() {
    return mActionApplications;
  }

  /**
   * Sets the actionapplications of the user
   *
   * @param pActionApplications
   */
  public void setActionApplications(Set<ActionApplication> pActionApplications) {
    this.mActionApplications = pActionApplications;
  }

  /**
   * Adds an actionapplication for the user
   *
   * @param pActionApplication
   */
  public void addActionApplication(Action pActionApplication) {
    this.mActionApplications.add(new ActionApplication(pActionApplication));
  }

  /**
   * Removes the action application for the given actionid
   *
   * @param pActionid
   */
  public void removeActionApplication(String pActionid) {
    for (Iterator<ActionApplication> iter = mActionApplications.iterator(); iter.hasNext();) {
      ActionApplication actionApplication = iter.next();
      if (StringUtils.equals(actionApplication.getAction(), pActionid)) {
        iter.remove();
        return;
      }
    }
  }

  @Override
  @JsonIgnore
  public boolean isValid() {
    return StringUtils.isNotBlank(mFullname)
            && StringUtils.isNotBlank(mName)
            && StringUtils.isNotBlank(mPassword)
            && StringUtils.isNotBlank(mMail);
  }

  @Override
  public DataType getDataType() {
    return super.getDataType() != null ? super.getDataType() : DataType.USER;
  }
}
